package org.example;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import test.ConvertTest;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;


public class Main {

    //    private static final String IP_ADDR = "192.168.51.114";
    private static final String IP_ADDR = "localhost";

    public static String login() {

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "\n{\r\n    \"userName\": \"sroot\",\r\n    \"userSecret\": \"3ea64b0770be9dbbf18133e9ff7c72b9797343710ab61a9fb4418fbcb4f4bab0dbeb51948247a3ac6f94513927db32df56fdfd905e94d2cd869463edee8076fb9088d15e74f585e2958d6bd8f06dc800150620042917c1761139a8a7471aee8e476d81b67cc86c899e4a6836307cf2d8e94a59360fb58c1cf59afae0c12a1515\"\r\n}");
        Request request = new Request.Builder()
                .url("http://" + IP_ADDR + ":8080/account/login")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Cookie", "satoken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiS0lUTlhIdlVVOTFJSk1iaUFtV3RhY3l4N3lhRHZNREUiLCJ1c2VyTmFtZSI6InNyb290IiwiZXhwaXJhdGlvblRpbWUiOiIyMDI0LTA3LTE5VDA0OjA2OjM0Ljc4MTE3MzgwMCJ9.lf23vMK8WsT5qVbiTCHTzVH9KPSVXvknZqE6KZ86WFM")
                .build();
        try {
            Response response = client.newCall(request).execute();

//            JSONObject jsonObj = JSON.parseObject(response.body().string());
//            String data = jsonObj.getString("data");
//            JSONObject tokenObj = JSON.parseObject(data);
//            response.close();
//            return tokenObj.getString("token");
            return response.headers().get("token");

        } catch (Exception e) {
            System.out.println("Login in failed");
        }
        ;
        return "";
    }

    public static String upCloud(String loginToken) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("groupId", "2")
                .build();
        Request request = new Request.Builder()
                .url("http://" + IP_ADDR + ":8080/terminal-service/v1/management/create")
                .method("POST", body).addHeader("Token", loginToken)
                .build();
        try {
            Response response = client.newCall(request).execute();
            JSONObject jsonObj = JSON.parseObject(response.body().string());

            String data = jsonObj.getString("data");
            JSONObject tokenObj = JSON.parseObject(data);
            response.close();
            return tokenObj.getString("token");
        } catch (Exception e) {
            System.out.println("Get upper cloud token failed");
            throw new RuntimeException(e);
        }
    }

    public static String getDevice(String loginToken, String upCloudToken) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        String bodyValue = "{\r\n    \"serialNo\": \"01ea6bf9-998a-4f4e-a76f-57872bb8d624\",\r\n    \"model\": \"PlayBox\", \n    \"token\": \"" + upCloudToken + "\"\r\n}";
        RequestBody body = RequestBody.create(mediaType, bodyValue);
        Request request = new Request.Builder()
                .url("http://" + IP_ADDR + ":8082/ledMaster-device/mqtt/device/register")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Token", loginToken)
                .build();
        try {
            Response response = client.newCall(request).execute();
            JSONObject jsonObj = JSON.parseObject(response.body().string());
            String data = jsonObj.getString("data");
            JSONObject tokenObj = JSON.parseObject(data);
            response.close();
            return tokenObj.getString("deviceName") + " " + tokenObj.getString("deviceSecret");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Get device information failed");
            throw new RuntimeException(e);
        }
    }

    private static boolean stingIsEmpty(CharSequence cs) {
        return cs != null && cs.length() != 0;
    }

    public static String getPassword(String deviceName,
                                     String productKey,
                                     String timeStamp,
                                     String envPrefix) {

        if (stingIsEmpty(deviceName)
                && stingIsEmpty(productKey)
                && stingIsEmpty(timeStamp)
                && stingIsEmpty(envPrefix)) {
            return "clientId" +
                    deviceName +
                    "deviceName" +
                    productKey +
                    "_" +
                    deviceName +
                    "productKey" +
                    envPrefix +
                    "timestamp" +
                    timeStamp;
        }
        ;
        return "";
    }

    public static String toHex(byte[] array) {
        BigInteger bi = new BigInteger(1, array);
        String hex = bi.toString(16);
        int paddingLength = (array.length * 2) - hex.length();
        if (paddingLength > 0) {
            return String.format("%0" + paddingLength + "d", 0) + hex;
        } else {
            return hex;
        }
    }

    public static String getHmacSign(String content, String key) {
        try {
            SecretKeySpec signKey = new SecretKeySpec(key.getBytes(), CommonConstants.HMAC_SHA1_ALGORITHM);
            Mac mac = Mac.getInstance(CommonConstants.HMAC_SHA1_ALGORITHM);
            mac.init(signKey);
            byte[] rawHmac;
            rawHmac = mac.doFinal(content.getBytes(StandardCharsets.UTF_8.toString()));
            return toHex(rawHmac);
        } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException |
                 UnsupportedEncodingException e) {
            return "";
        }

    }

    public static String getDeviceName(String envPrefix, String productKey, String deviceName) {

        if (stingIsEmpty(envPrefix)
                && stingIsEmpty(productKey)
                && stingIsEmpty(deviceName)) {
            return envPrefix +
                    CommonConstants.CONNECTOR4 +
                    productKey +
                    CommonConstants.CONNECTOR5 +
                    deviceName;
        }
        return "";
    }

    public static String getClientId(String deviceName, String timeStamp) {
        if (stingIsEmpty(deviceName) && stingIsEmpty(timeStamp)) {
            return deviceName +
                    CommonConstants.CONNECTOR1 +
                    CommonConstants.SECURE_MODE +
                    CommonConstants.CONNECTOR2 +
                    CommonConstants.SECURE_MODE_VALUE +
                    CommonConstants.CONNECTOR3 +
                    CommonConstants.SIGN_METHOD +
                    CommonConstants.CONNECTOR2 +
                    CommonConstants.HMAC_SHA1_ALGORITHM +
                    CommonConstants.CONNECTOR3 +
                    CommonConstants.TIMESTAMP +
                    CommonConstants.CONNECTOR2 +
                    timeStamp +
                    CommonConstants.CONNECTOR1;
        }
        return "";
    }

    public static void PostUpCloud(String name, String pwd, String timeStamp, String uuid) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n    \"screenName\": \"AutoTestPlayBox" + name + "\",\r\n    \n    \n    \n    \"uniqueId\": \"" + uuid + "\"\r\n}\r\n");
        Request request = new Request.Builder()
                .url("http://" + IP_ADDR + ":8082/ledMaster-device/mqtt/playbox/screen/reported")
                .method("POST", body)
                .addHeader("device-name", name)
                .addHeader("device-timestamp", timeStamp)
                .addHeader("device-password", pwd)
                .addHeader("Content-Type", "application/json")
                .addHeader("Cookie", "satoken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiS0lUTlhIdlVVOTFJSk1iaUFtV3RhY3l4N3lhRHZNREUiLCJ1c2VyTmFtZSI6InNyb290IiwiZXhwaXJhdGlvblRpbWUiOiIyMDI0LTA3LTE5VDA0OjA2OjM0Ljc4MTE3MzgwMCJ9.lf23vMK8WsT5qVbiTCHTzVH9KPSVXvknZqE6KZ86WFM")
                .build();
        try {
            Response response = client.newCall(request).execute();
            response.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("screen upper cloud failed");
            throw new RuntimeException(e);
        }

    }

    public static String GetUser(String loginToken) {
        String upCloudToken = upCloud(loginToken);
        System.out.printf("upCloudToken: %s\n", upCloudToken);
        String[] deviceInfo = getDevice(loginToken, upCloudToken).split(" ");
        String deviceName = deviceInfo[0];
        String deviceSecret = deviceInfo[1];
        System.out.printf("deviceName: %s\n", deviceName);
        System.out.printf("deviceSecret: %s\n", deviceSecret);
        String productKey = "PlayBox";
        String envPrefix = "COLORLIGHT10";
        //显示屏在云上展示
        String[] threeGroup = {deviceName, productKey, deviceSecret};
//        ScreenUpCloud screen = new ScreenUpCloud();
        String params = ScreenUpCloud.main(threeGroup);
        String[] pwdTimeStamp = params.split(" ");
        String upCloudPwd = pwdTimeStamp[0];
        String upCloudTimeStamp = pwdTimeStamp[1];
        //调用上云接口
        String uuid = getUUID32();
        PostUpCloud(deviceName, upCloudPwd, upCloudTimeStamp, uuid);


        long l = System.currentTimeMillis();
        String timeStamp = String.valueOf(l);

        String password = getPassword(
                deviceName,
                productKey,
                timeStamp,
                envPrefix);
        System.out.println("明文密码:" + password);
        char[] chars = getHmacSign((password),
                deviceSecret
        ).toCharArray();
        String userName = getDeviceName(envPrefix, productKey, deviceName);
        String pwd = String.valueOf(chars);
        String clientId = getClientId(deviceName, timeStamp);
        System.out.println("username:" + userName);
        System.out.println("password:" + pwd);
        System.out.println("ClientId:" + clientId);

        String result = deviceName + ";" + userName + ";" + pwd + ";" + clientId;
        //HB
        byte[] preHBReport = new byte[]{8, -118, 14, 32, -66, -46, -5, -112, -120, 50, 42, 6, 49, 46, 53, 49, 46, 53, 56, -24, 7};
//        byte[] postHBReport  = ConvertTest.convertBytes(preHBReport, uuid);
        String hexHBReport = toHex(preHBReport);
        System.out.printf("hexHBReport: %s\n", hexHBReport);

//        新协议播放盒上报
        byte[] prePlayerReport = new byte[]{8, -118, 10, 26, -98, 13, 10, -101, 13, 18, 12, 67, 76, 67, 65, 50, 48, 49, 49, 50, 51, 53, 48, 26, 4, 97, 50, 48, 48, 34, 12, 84, 101, 114, 109, 105, 110, 97, 108, 50, 51, 53, 48, 42, 42, 10, 6, 49, 46, 55, 49, 46, 54, 18, 32, 97, 50, 48, 48, 32, 49, 46, 48, 48, 40, 32, 77, 97, 105, 110, 45, 70, 80, 71, 65, 58, 50, 46, 48, 49, 40, 54, 54, 54, 54, 41, 41, 48, -128, -48, -116, 1, 56, -118, -20, -79, 7, 64, 20, 74, 34, 8, 1, 34, 2, 16, 1, 42, 2, 24, 100, 50, 3, 24, -28, 50, 66, 17, -26, -106, -80, -27, -69, -70, -24, -118, -126, -25, -101, -82, 55, 46, 118, 115, 110, 82, 24, 8, -128, -32, -40, -65, 99, 16, -128, -128, -128, -121, 102, 24, -128, -64, -43, -73, 2, 32, -128, -64, -45, -36, 3, 90, -54, 1, 10, 41, 8, 1, 18, 6, 108, 97, 116, 97, 116, 97, 26, 8, 51, 89, 75, 107, 36, 68, 108, 113, 34, 17, 55, 101, 58, 56, 55, 58, 99, 101, 58, 98, 100, 58, 53, 54, 58, 50, 57, 40, 6, 18, 107, 8, 1, 18, 101, 10, 72, 10, 14, 49, 57, 50, 46, 49, 54, 56, 46, 53, 49, 46, 49, 49, 57, 18, 14, 49, 57, 50, 46, 49, 54, 56, 46, 53, 49, 46, 50, 53, 52, 26, 13, 50, 53, 53, 46, 50, 53, 53, 46, 50, 53, 53, 46, 48, 34, 14, 49, 57, 50, 46, 49, 54, 56, 46, 53, 49, 46, 50, 53, 52, 42, 7, 48, 46, 48, 46, 48, 46, 48, 18, 6, 115, 116, 97, 116, 105, 99, 26, 17, 48, 97, 58, 57, 99, 58, 48, 56, 58, 98, 50, 58, 52, 100, 58, 99, 98, 24, 1, 26, 37, 18, 29, 10, 0, 18, 6, 115, 116, 97, 116, 105, 99, 26, 17, 55, 99, 58, 56, 55, 58, 99, 101, 58, 98, 100, 58, 53, 54, 58, 50, 57, 32, -1, -1, -1, -1, 15, 34, 9, 58, 7, 85, 78, 75, 78, 79, 87, 78, 98, 37, 10, 13, 8, -24, 15, 16, 7, 24, 5, 32, 15, 40, 11, 48, 48, 18, 13, 65, 115, 105, 97, 47, 83, 104, 97, 110, 103, 104, 97, 105, 29, 0, 0, 0, 65, 32, 1, 106, 4, 24, 2, 32, 2, 114, 38, 18, 6, 40, -128, 10, 48, -128, 4, 18, 8, 8, 1, 40, -128, 6, 48, -128, 2, 18, 8, 8, 2, 40, -128, 10, 48, -128, 4, 18, 8, 8, 3, 40, -128, 10, 48, -128, 4, 122, 15, 10, 8, 49, 50, 56, 48, 120, 53, 49, 50, 18, 3, 48, 120, 48, -126, 1, 0, -118, 1, -62, 9, 16, 1, 24, 25, 32, 7, 42, 4, 8, 100, 16, 100, 50, 0, 58, -41, 4, 0, 0, 0, 0, 2, 3, 5, 8, 11, 15, 19, 25, 31, 38, 46, 54, 64, 75, 86, 99, 112, 127, -113, 1, -96, 1, -78, 1, -59, 1, -39, 1, -17, 1, -123, 2, -99, 2, -73, 2, -47, 2, -19, 2, -118, 3, -87, 3, -55, 3, -22, 3, -115, 4, -79, 4, -41, 4, -2, 4, -89, 5, -47, 5, -3, 5, -86, 6, -39, 6, -119, 7, -69, 7, -17, 7, -92, 8, -37, 8, -108, 9, -50, 9, -118, 10, -56, 10, -121, 11, -55, 11, -116, 12, -48, 12, -105, 13, -33, 13, -86, 14, -10, 14, -60, 15, -108, 16, -27, 16, -71, 17, -113, 18, -26, 18, -64, 19, -101, 20, -8, 20, -40, 21, -71, 22, -99, 23, -126, 24, -22, 24, -45, 25, -65, 26, -84, 27, -100, 28, -114, 29, -126, 30, -7, 30, -15, 31, -20, 32, -24, 33, -25, 34, -24, 35, -20, 36, -15, 37, -7, 38, -125, 40, -112, 41, -98, 42, -81, 43, -61, 44, -40, 45, -16, 46, -118, 48, -89, 49, -58, 50, -25, 51, -117, 53, -79, 54, -38, 55, -123, 57, -78, 58, -30, 59, -108, 61, -55, 62, -128, 64, -70, 65, -10, 66, -75, 68, -10, 69, -70, 71, -127, 73, -54, 74, -107, 76, -29, 77, -76, 79, -121, 81, -35, 82, -74, 84, -111, 86, -17, 87, -49, 89, -78, 91, -104, 93, -127, 95, -20, 96, -38, 98, -53, 100, -66, 102, -76, 104, -83, 106, -87, 108, -89, 110, -88, 112, -84, 114, -77, 116, -67, 118, -55, 120, -40, 122, -22, 124, -1, 126, -105, -127, 1, -78, -125, 1, -49, -123, 1, -16, -121, 1, -109, -118, 1, -71, -116, 1, -30, -114, 1, -114, -111, 1, -67, -109, 1, -17, -107, 1, -92, -104, 1, -36, -102, 1, -105, -99, 1, -43, -97, 1, -106, -94, 1, -39, -92, 1, -96, -89, 1, -22, -87, 1, -73, -84, 1, -121, -81, 1, -38, -79, 1, -80, -76, 1, -119, -73, 1, -27, -71, 1, -59, -68, 1, -89, -65, 1, -116, -62, 1, -11, -60, 1, -31, -57, 1, -48, -54, 1, -62, -51, 1, -73, -48, 1, -81, -45, 1, -86, -42, 1, -87, -39, 1, -85, -36, 1, -80, -33, 1, -72, -30, 1, -60, -27, 1, -46, -24, 1, -28, -21, 1, -7, -18, 1, -111, -14, 1, -83, -11, 1, -52, -8, 1, -18, -5, 1, -109, -1, 1, -68, -126, 2, -24, -123, 2, -104, -119, 2, -54, -116, 2, -128, -112, 2, -71, -109, 2, -10, -106, 2, -74, -102, 2, -7, -99, 2, -64, -95, 2, -118, -91, 2, -41, -88, 2, -88, -84, 2, -4, -81, 2, -44, -77, 2, -81, -73, 2, -115, -69, 2, -17, -66, 2, -43, -62, 2, -67, -58, 2, -86, -54, 2, -103, -50, 2, -115, -46, 2, -125, -42, 2, -3, -39, 2, -5, -35, 2, -4, -31, 2, -127, -26, 2, -119, -22, 2, -108, -18, 2, -92, -14, 2, -74, -10, 2, -51, -6, 2, -25, -2, 2, -124, -125, 3, -91, -121, 3, -54, -117, 3, -14, -113, 3, -99, -108, 3, -51, -104, 3, -128, -99, 3, -74, -95, 3, -15, -91, 3, -82, -86, 3, -16, -82, 3, -75, -77, 3, -2, -73, 3, -54, -68, 3, -101, -63, 3, -18, -59, 3, -58, -54, 3, -95, -49, 3, -128, -44, 3, -29, -40, 3, -55, -35, 3, -77, -30, 3, -95, -25, 3, -109, -20, 3, -120, -15, 3, -127, -10, 3, -2, -6, 3, -1, -1, 3, 66, -41, 4, 0, 0, 0, 0, 2, 3, 5, 8, 11, 15, 19, 25, 31, 38, 46, 54, 64, 75, 86, 99, 112, 127, -113, 1, -96, 1, -78, 1, -59, 1, -39, 1, -17, 1, -123, 2, -99, 2, -73, 2, -47, 2, -19, 2, -118, 3, -87, 3, -55, 3, -22, 3, -115, 4, -79, 4, -41, 4, -2, 4, -89, 5, -47, 5, -3, 5, -86, 6, -39, 6, -119, 7, -69, 7, -17, 7, -92, 8, -37, 8, -108, 9, -50, 9, -118, 10, -56, 10, -121, 11, -55, 11, -116, 12, -48, 12, -105, 13, -33, 13, -86, 14, -10, 14, -60, 15, -108, 16, -27, 16, -71, 17, -113, 18, -26, 18, -64, 19, -101, 20, -8, 20, -40, 21, -71, 22, -99, 23, -126, 24, -22, 24, -45, 25, -65, 26, -84, 27, -100, 28, -114, 29, -126, 30, -7, 30, -15, 31, -20, 32, -24, 33, -25, 34, -24, 35, -20, 36, -15, 37, -7, 38, -125, 40, -112, 41, -98, 42, -81, 43, -61, 44, -40, 45, -16, 46, -118, 48, -89, 49, -58, 50, -25, 51, -117, 53, -79, 54, -38, 55, -123, 57, -78, 58, -30, 59, -108, 61, -55, 62, -128, 64, -70, 65, -10, 66, -75, 68, -10, 69, -70, 71, -127, 73, -54, 74, -107, 76, -29, 77, -76, 79, -121, 81, -35, 82, -74, 84, -111, 86, -17, 87, -49, 89, -78, 91, -104, 93, -127, 95, -20, 96, -38, 98, -53, 100, -66, 102, -76, 104, -83, 106, -87, 108, -89, 110, -88, 112, -84, 114, -77, 116, -67, 118, -55, 120, -40, 122, -22, 124, -1, 126, -105, -127, 1, -78, -125, 1, -49, -123, 1, -16, -121, 1, -109, -118, 1, -71, -116, 1, -30, -114, 1, -114, -111, 1, -67, -109, 1, -17, -107, 1, -92, -104, 1, -36, -102, 1, -105, -99, 1, -43, -97, 1, -106, -94, 1, -39, -92, 1, -96, -89, 1, -22, -87, 1, -73, -84, 1, -121, -81, 1, -38, -79, 1, -80, -76, 1, -119, -73, 1, -27, -71, 1, -59, -68, 1, -89, -65, 1, -116, -62, 1, -11, -60, 1, -31, -57, 1, -48, -54, 1, -62, -51, 1, -73, -48, 1, -81, -45, 1, -86, -42, 1, -87, -39, 1, -85, -36, 1, -80, -33, 1, -72, -30, 1, -60, -27, 1, -46, -24, 1, -28, -21, 1, -7, -18, 1, -111, -14, 1, -83, -11, 1, -52, -8, 1, -18, -5, 1, -109, -1, 1, -68, -126, 2, -24, -123, 2, -104, -119, 2, -54, -116, 2, -128, -112, 2, -71, -109, 2, -10, -106, 2, -74, -102, 2, -7, -99, 2, -64, -95, 2, -118, -91, 2, -41, -88, 2, -88, -84, 2, -4, -81, 2, -44, -77, 2, -81, -73, 2, -115, -69, 2, -17, -66, 2, -43, -62, 2, -67, -58, 2, -86, -54, 2, -103, -50, 2, -115, -46, 2, -125, -42, 2, -3, -39, 2, -5, -35, 2, -4, -31, 2, -127, -26, 2, -119, -22, 2, -108, -18, 2, -92, -14, 2, -74, -10, 2, -51, -6, 2, -25, -2, 2, -124, -125, 3, -91, -121, 3, -54, -117, 3, -14, -113, 3, -99, -108, 3, -51, -104, 3, -128, -99, 3, -74, -95, 3, -15, -91, 3, -82, -86, 3, -16, -82, 3, -75, -77, 3, -2, -73, 3, -54, -68, 3, -101, -63, 3, -18, -59, 3, -58, -54, 3, -95, -49, 3, -128, -44, 3, -29, -40, 3, -55, -35, 3, -77, -30, 3, -95, -25, 3, -109, -20, 3, -120, -15, 3, -127, -10, 3, -2, -6, 3, -1, -1, 3, -110, 1, 4, 18, 2, 16, 10, 32, -73, -35, -16, -115, -120, 50, 42, 6, 49, 46, 53, 49, 46, 53, 50, 36, 56, 52, 56, 98, 55, 52, 101, 100, 45, 97, 56, 54, 55, 45, 52, 55, 56, 51, 45, 57, 49, 49, 53, 45, 51, 51, 51, 101, 98, 49, 52, 56, 48, 55, 50, 55, 56, -21, 7};
        byte[] postPlayerReport = ConvertTest.convertBytes(prePlayerReport, uuid);
        String hexPlayerReport = toHex(postPlayerReport);
        result += ";" + hexPlayerReport;
//        System.out.printf("hexPlayerReport: %s\n",hexPlayerReport);

//        新协议传感器上报
        byte[] prePlayerSensorReport = new byte[]{8, -22, 11, 26, 6, 10, 4, 10, 2, 24, 27, 32, -59, -127, -99, -113, -120, 50, 42, 6, 49, 46, 53, 49, 46, 53, 50, 36, 56, 52, 56, 98, 55, 52, 101, 100, 45, 97, 56, 54, 55, 45, 52, 55, 56, 51, 45, 57, 49, 49, 53, 45, 51, 51, 51, 101, 98, 49, 52, 56, 48, 55, 50, 55, 56, -6, 7};
        byte[] postPlayerSensorReport = ConvertTest.convertBytes(prePlayerSensorReport, uuid);
        String hexPlayerSensorReport = toHex(postPlayerSensorReport);
        result += ";" + hexPlayerSensorReport;

//        新协议接收卡上报
        byte[] preReceiverReport = new byte[]{8, -38, 11, 26, -97, 4, 10, -106, 4, 18, 0, 18, -106, 3, 8, 1, 18, -128, 1, 18, 8, 75, 57, 43, 32, 56, 46, 50, 48, 34, 22, 10, 2, 53, 53, 18, 7, 83, 77, 49, 54, 51, 56, 48, 24, -128, 1, 32, 64, 40, 64, 48, 64, 42, 6, 24, -128, 2, 32, -128, 2, 50, 84, 8, -59, -49, 15, 18, 4, 16, -59, -49, 15, 18, 2, 8, 1, 24, -125, 13, 32, -88, -67, 3, 40, -44, -61, 3, 57, 127, 73, 26, 3, 122, -65, 75, 63, 64, 1, 74, 2, 16, 1, 74, 4, 8, 1, 16, 1, 82, 4, 16, 2, 24, 1, 82, 6, 8, 1, 16, 2, 24, 1, 82, 6, 8, 2, 16, 2, 24, 1, 82, 6, 8, 3, 16, 2, 24, 1, 82, 6, 8, 4, 16, 2, 24, 1, 18, -123, 1, 8, 1, 18, 8, 75, 57, 43, 32, 56, 46, 50, 48, 34, 22, 10, 2, 53, 53, 18, 6, 77, 89, 57, 56, 54, 56, 24, -128, 1, 32, -128, 8, 40, 64, 48, 32, 42, 9, 8, -128, 2, 24, -128, 2, 32, -128, 2, 50, 84, 8, -59, -49, 15, 18, 4, 16, -59, -49, 15, 18, 2, 8, 1, 24, -125, 13, 32, -32, -44, 3, 40, -44, -40, 3, 57, -79, -63, -122, 11, -89, -86, 83, 63, 64, 1, 74, 2, 16, 1, 74, 4, 8, 1, 16, 1, 82, 4, 16, 2, 24, 1, 82, 6, 8, 1, 16, 2, 24, 1, 82, 6, 8, 2, 16, 2, 24, 1, 82, 6, 8, 3, 16, 2, 24, 1, 82, 6, 8, 4, 16, 2, 24, 1, 18, -122, 1, 8, 2, 18, 9, 105, 57, 43, 32, 49, 49, 46, 51, 54, 34, 23, 10, 2, 53, 51, 18, 7, 73, 67, 78, 50, 48, 53, 51, 24, -128, 2, 32, -128, 2, 40, 16, 48, 32, 42, 9, 8, -128, 4, 24, -128, 2, 32, -128, 2, 50, 83, 18, 0, 18, 2, 8, 1, 24, -125, 13, 32, -104, -32, 7, 40, -15, -79, -1, -1, -1, -1, -1, -1, -1, 1, 57, -11, -117, -65, 25, -81, -109, 71, 63, 64, 1, 74, 2, 16, 1, 74, 4, 8, 1, 16, 1, 82, 4, 16, 1, 24, 1, 82, 6, 8, 1, 16, 1, 24, 1, 82, 6, 8, 2, 16, 2, 24, 1, 82, 6, 8, 3, 16, 2, 24, 1, 82, 6, 8, 4, 16, 2, 24, 1, 18, 117, 8, 2, 18, 113, 18, 9, 75, 53, 43, 32, 49, 56, 46, 49, 54, 34, 22, 10, 2, 53, 48, 18, 7, 67, 70, 68, 51, 50, 53, 65, 24, -112, 1, 32, 72, 40, 72, 48, 18, 42, 4, 24, 64, 32, 64, 50, 70, 18, 0, 18, 2, 8, 1, 24, -125, 13, 57, -14, 20, 40, 24, -124, -88, 113, 63, 64, 1, 74, 2, 16, 1, 74, 4, 8, 1, 16, 1, 82, 4, 16, 2, 24, 1, 82, 6, 8, 1, 16, 2, 24, 1, 82, 6, 8, 2, 16, 2, 24, 1, 82, 6, 8, 3, 16, 2, 24, 1, 82, 6, 8, 4, 16, 2, 24, 1, 88, 1, 18, 2, 8, 3, 58, 4, 16, 1, 32, 50, 32, -79, -26, -112, -113, -120, 50, 42, 6, 49, 46, 53, 49, 46, 53, 50, 36, 56, 52, 56, 98, 55, 52, 101, 100, 45, 97, 56, 54, 55, 45, 52, 55, 56, 51, 45, 57, 49, 49, 53, 45, 51, 51, 51, 101, 98, 49, 52, 56, 48, 55, 50, 55, 56, -22, 7};
        byte[] postReceiverReport = ConvertTest.convertBytes(preReceiverReport, uuid);
        String hexReceiverReport = toHex(postReceiverReport);
        result += ";" + hexReceiverReport;

//        新协议模组上报
        byte[] preSmartModuleMonitorReport = new byte[]{8, -110, 13, 26, -55, 2, 10, 19, 10, 2, 16, 1, 26, 6, 10, 4, 48, 46, 48, 48, 34, 5, 32, -96, 1, 40, 120, 10, 24, 10, 4, 16, 1, 32, 1, 26, 6, 10, 4, 48, 46, 48, 48, 34, 8, 24, -96, 1, 32, -64, 2, 40, 120, 10, 24, 10, 4, 16, 1, 32, 2, 26, 6, 10, 4, 48, 46, 48, 48, 34, 8, 16, 120, 32, -96, 1, 40, -16, 1, 10, 27, 10, 4, 16, 1, 32, 3, 26, 6, 10, 4, 48, 46, 48, 48, 34, 11, 16, 120, 24, -96, 1, 32, -64, 2, 40, -16, 1, 10, 25, 10, 4, 16, 1, 32, 4, 26, 6, 10, 4, 48, 46, 48, 48, 34, 9, 16, -16, 1, 32, -96, 1, 40, -24, 2, 10, 28, 10, 4, 16, 1, 32, 5, 26, 6, 10, 4, 48, 46, 48, 48, 34, 12, 16, -16, 1, 24, -96, 1, 32, -64, 2, 40, -24, 2, 10, 25, 10, 4, 16, 1, 32, 6, 26, 6, 10, 4, 48, 46, 48, 48, 34, 9, 16, -24, 2, 32, -96, 1, 40, -32, 3, 10, 28, 10, 4, 16, 1, 32, 7, 26, 6, 10, 4, 48, 46, 48, 48, 34, 12, 16, -24, 2, 24, -96, 1, 32, -64, 2, 40, -32, 3, 10, 52, 10, 4, 16, 2, 48, 1, 18, 26, 8, -9, -4, 1, 16, -15, -79, -1, -1, -1, -1, -1, -1, -1, 1, 24, -15, 35, 32, -1, -1, -1, -1, 15, 40, 1, 26, 8, 10, 4, 49, 46, 48, 48, 16, 1, 34, 6, 8, 1, 32, 72, 40, 72, 10, 57, 10, 6, 16, 2, 32, 1, 48, 1, 18, 26, 8, -9, -4, 1, 16, -15, -79, -1, -1, -1, -1, -1, -1, -1, 1, 24, -123, 36, 32, -1, -1, -1, -1, 15, 40, 1, 26, 8, 10, 4, 49, 46, 48, 48, 16, 1, 34, 9, 8, 1, 24, 72, 32, -112, 1, 40, 72, 32, -124, -78, -97, -112, -120, 50, 42, 6, 49, 46, 53, 49, 46, 53, 50, 36, 56, 52, 56, 98, 55, 52, 101, 100, 45, 97, 56, 54, 55, 45, 52, 55, 56, 51, 45, 57, 49, 49, 53, 45, 51, 51, 51, 101, 98, 49, 52, 56, 48, 55, 50, 55, 56, -9, 7};
        byte[] postSmartModuleMonitorReport = ConvertTest.convertBytes(preSmartModuleMonitorReport, uuid);
        String hexSmartModuleMonitorReport = toHex(postSmartModuleMonitorReport);
        result += ";" + hexSmartModuleMonitorReport;

//       新协议M2&多功能卡上报
        byte[] preFunctionReport = new byte[]{8, -28, 11, 26, -45, 3, 10, 97, 10, 2, 77, 50, 18, 6, 8, -72, -10, 10, 24, 22, 18, 6, 8, -84, -29, 1, 24, 1, 18, 6, 8, -96, -106, 3, 24, 2, 26, 4, 8, 1, 24, 1, 26, 4, 8, 2, 24, 2, 26, 4, 8, 3, 24, 1, 34, 15, 16, 1, 24, 1, 48, 1, 56, 80, 64, 70, 72, 1, 80, -12, 3, 34, 15, 16, 2, 24, 1, 48, 1, 56, 80, 64, 70, 72, 1, 80, -12, 3, 34, 15, 16, 3, 24, 1, 48, 1, 56, 80, 64, 70, 72, 1, 80, -12, 3, 18, -19, 2, 18, -22, 2, 26, 6, 8, -35, -61, 2, 24, 1, 26, 5, 8, -53, 76, 24, 2, 26, 6, 8, -108, -46, 1, 24, 20, 26, 6, 8, -20, -126, 4, 24, 21, 26, 6, 8, -88, -61, 1, 24, 25, 26, 6, 8, -112, -53, 1, 24, 26, 74, 3, 73, 77, 57, 82, 8, 73, 77, 57, 32, 49, 51, 46, 48, 88, 1, -126, 1, 6, 10, 2, 74, 57, 16, 1, -126, 1, 7, 10, 3, 74, 49, 48, 16, 1, -126, 1, 7, 10, 3, 74, 49, 49, 16, 1, -126, 1, 7, 10, 3, 74, 49, 50, 16, 1, -126, 1, 7, 10, 3, 74, 49, 51, 16, 1, -126, 1, 7, 10, 3, 74, 49, 52, 16, 1, -126, 1, 7, 10, 3, 74, 49, 53, 16, 1, -126, 1, 7, 10, 3, 74, 49, 54, 16, 1, -118, 1, 16, 10, 7, 74, 57, 116, 111, 74, 49, 50, 34, 0, 40, 60, 48, -40, 4, -118, 1, 14, 10, 3, 74, 49, 51, 16, 1, 34, 0, 40, 60, 48, -40, 4, -118, 1, 12, 10, 3, 74, 49, 52, 34, 0, 40, 60, 48, -40, 4, -118, 1, 12, 10, 3, 74, 49, 53, 34, 0, 40, 60, 48, -40, 4, -118, 1, 12, 10, 3, 74, 49, 54, 34, 0, 40, 60, 48, -40, 4, -110, 1, 91, 10, 7, 74, 57, 116, 111, 74, 49, 50, 16, 1, 26, 28, 8, 1, 24, 1, 34, 7, 8, -24, 15, 16, 7, 24, 1, 42, 7, 8, -24, 15, 16, 8, 24, 1, 48, 39, 104, 16, 112, 25, 26, 18, 8, 1, 16, 1, 34, 3, 8, -49, 17, 42, 3, 8, -49, 17, 104, 17, 112, 1, 26, 28, 8, 1, 24, 1, 34, 7, 8, -24, 15, 16, 7, 24, 2, 42, 7, 8, -24, 15, 16, 7, 24, 2, 48, 39, 104, 17, 112, 3, -110, 1, 5, 10, 3, 74, 49, 51, -110, 1, 5, 10, 3, 74, 49, 52, -110, 1, 5, 10, 3, 74, 49, 53, -94, 1, 17, 10, 13, 8, -24, 15, 16, 7, 24, 5, 32, 15, 40, 58, 48, 2, 16, 5, 32, -49, -128, -103, -113, -120, 50, 42, 6, 49, 46, 53, 49, 46, 53, 50, 36, 56, 52, 56, 98, 55, 52, 101, 100, 45, 97, 56, 54, 55, 45, 52, 55, 56, 51, 45, 57, 49, 49, 53, 45, 51, 51, 51, 101, 98, 49, 52, 56, 48, 55, 50, 55, 56, -20, 7};
        byte[] postFunctionReport = ConvertTest.convertBytes(preFunctionReport, uuid);
        String hexFunctionReport = toHex(postFunctionReport);
        result += ";" + hexFunctionReport;

        return result;
    }

    public static void main(String[] args) throws InterruptedException {

        final String loginToken = login();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        AtomicInteger length = new AtomicInteger();
        while (true) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    GetUser(loginToken);
                }
            });
            length.getAndIncrement();
            if (length.get() % 1000000 == 0) {
                break;
            }
        }
        pool.shutdown();
        System.out.println("end");
//        System.out.printf("loginToken: %s\n",loginToken);

//        第一步：设置输出的文件路径
        //如果该目录下不存在该文件，则文件会被创建到指定目录下。如果该目录有同名文件，那么该文件将被覆盖。
//        File writeFile = new File("E:\\job\\压测\\batch_usr_pwd_local_1000.csv");
//
//        try{
//            //第二步：通过BufferedReader类创建一个使用默认大小输出缓冲区的缓冲字符输出流
//            BufferedWriter writeText = new BufferedWriter(new FileWriter(writeFile,true));
//            String lineText = null;
//            //第三步：将文档的下一行数据赋值给lineData，并判断是否为空，若不为空则输出
//            for(int i=1;i<=9999;i++){
//                //调用write的方法将字符串写到流中
//                lineText = String.valueOf(i)+";"+GetUser(loginToken);
//                writeText.write(lineText);
//                writeText.newLine();
//                System.out.printf("第%d个用户",i);
//
//            }
//
//            //使用缓冲区的刷新方法将数据刷到目的地中
//            writeText.flush();
//            //关闭缓冲区，缓冲区没有调用系统底层资源，真正调用底层资源的是FileWriter对象，缓冲区仅仅是一个提高效率的作用
//            //因此，此处的close()方法关闭的是被缓存的流对象
//            writeText.close();
//        }catch (Exception e){
//            e.printStackTrace();
//        }


    }

    public static String getUUID32() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }


}

